<!DOCTYPE html>
<html>
<head>
	<title>深拷贝</title>
</head>
<body>
<script type="text/javascript">
	
/* 深度拷贝

	利用递归实现深度拷贝
*/

var user = {
	name: '张三',
	age: 18,
	dog: {
		name: '可可',
		age: 4
	}
}
function copyObj(o, n) {
	for (var key in o) {
		if (o[key] instanceof Object) {
			n[key] = {};
			copyObj(o[key], n[key]);
		} else if (o[key] instanceof Array) {
			n[key] = [];
			copyObj(o[key], n[key]);
		} else {
			n[key] = o[key];
		}
	}
}
var usera = {};
copyObj(user, usera);
usera.name = '李斯';
console.log(user);			// user.name并没有发生变化
usera.dog.name = '李斯';
console.log(user);			// user.dog.name并没有发生变化

</script>
</body>
</html>